package cn.codelead.masterdb.config;

import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

/**
 * @Author：wwf
 * @Package：cn.codelead.masterdb.config
 * @Project：mcp
 * @name：ModeConfig
 * @Date：2025/4/14 17:24
 * @Filename：ModeConfig
 */
@Configuration
public class ModeConfig {

    @Bean
    CommandLineRunner ingestTermOfServiceToVectorStore(VectorStore vectorStore,
                                                       @Value("classpath:rag/Prompt.txt") Resource termsOfServiceDocs) {
        return args -> {
            vectorStore.write(                                  // 3.写入向量数据库
                    new TokenTextSplitter().transform(          // 2.分隔、向量化
                            new TextReader(termsOfServiceDocs).read())  // 1.读取文本
            );
        };
    }

    /**
     * 记忆
     *
     * @return
     */
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }


    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore.SimpleVectorStoreBuilder builder = SimpleVectorStore.builder(embeddingModel);
        return builder.build();
    }


}
